﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GiveMeMenu.Common.Cache
{

    public class KeyEndNode<TKey,TValue>
    {
        /// <summary>
        /// In-memory cache dictionary
        /// </summary>
        private static Dictionary<TKey, IList<TValue>> _cache;
        private static object syncLock = new object();

        /// <summary>
        /// Cache initializer
        /// </summary>
        public KeyEndNode()
        {
            if (_cache == null)
            {
                lock (syncLock)
                {
                    if (_cache == null)
                    {
                        _cache = new Dictionary<TKey, IList<TValue>>();
                    }
                }
            }
        }


        /// <summary>
        /// Existses the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public bool Exists(TKey key) 
        {
            lock (syncLock)
            {
                return _cache.ContainsKey(key);
            }
        }


        /// <summary>
        /// Gets the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException">An object of the desired type does not exist: </exception>
        public IList<TValue> Get(TKey key)
        {
            lock (syncLock)
            {
                if (_cache.ContainsKey(key) == false)
                {
                    return null;
                }
                lock (syncLock)
                {
                    return _cache[key];

                }
            }
        }



        /// <summary>
        /// Adds the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="System.ApplicationException"></exception>
        public  void Add(TKey key, IList<TValue> value)
        {
            lock (syncLock)
            {
                if (_cache.ContainsKey(key))
                {
                    throw new ApplicationException("An object already exists");
                }
                lock (syncLock)
                {
                    _cache.Add(key , value);
                }
            }

        }

        /// <summary>
        /// Updates the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public  void Update(TKey key, IList<TValue> value)
        {
            lock (syncLock)
            {
                if (_cache.ContainsKey(key))
                {
                    lock (syncLock)
                    {
                        _cache[key] = value;
                    }
                }
                else
                {
                    lock (syncLock)
                    {
                        _cache.Add(key, value);
                    }
                }
            }

        }


        /// <summary>
        /// Removes the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <exception cref="System.ApplicationException"></exception>
        public void Remove(TKey key)
        {
            lock (syncLock)
            {
                if (_cache.ContainsKey(key) == false)
                {
                    throw new ApplicationException("An object does not exists in cache");
                }
                lock (syncLock)
                {
                    _cache.Remove(key);
                }
            }
        }

        /// <summary>
        /// Clears the cache.
        /// </summary>
        public void ClearCache() 
        {
            lock (syncLock)
            {
                _cache.Clear();
            }
        }

    }
}
